package com.example.huichaoliu;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.ListView;
import android.widget.TextView;

import com.huichaoliu.app.R;
import com.rederxu.farmer.BaseActivity;
import com.rederxu.tools.DisplayUtils;

public class DesignerDetailActivity extends BaseActivity implements OnClickListener {

	private ListView listView = null;
	private View headerView = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_design_detail);
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.left_button:
			finish();
			break;
		}
	}

	@Override
	public void init() {
		((TextView) findViewById(R.id.title_text)).setText("搭配师详情");
		findViewById(R.id.left_button).setOnClickListener(this);
		
		listView = (ListView) findViewById(R.id.listView);
		headerView = LayoutInflater.from(mContext).inflate(R.layout.header_designer_detail, null);
	}

	@Override
	public void initData() {
		listView.addHeaderView(headerView);
		listView.setAdapter(new Adapter());
	}

	@Override
	public void onNetworkChanged(boolean isMobNetConnected,
			boolean isWifiConnected) {
		// TODO Auto-generated method stub

	}

	private class Adapter extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return 10;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if(convertView == null){
				convertView = LayoutInflater.from(mContext).inflate(R.layout.item_designer_detail, null);
			}
			TextView tv_date = (TextView)convertView.findViewById(R.id.tv_date);
			GridView gridView = (GridView) convertView.findViewById(R.id.gridView);
			int width = DisplayUtils.getScreenWidth(mContext) - tv_date.getWidth() - DisplayUtils.dipToPx(mContext, 12) * 3;
			int w = (width - DisplayUtils.dipToPx(mContext, 8) * 2) / 3;
			int count = 4;
			int row = count / 3 + (count % 3 == 0 ? 0 : 1);
			int height = w * row + (row > 0 ? (row - 1) * DisplayUtils.dipToPx(mContext, 8) : 0);
			android.widget.LinearLayout.LayoutParams lp = new android.widget.LinearLayout.LayoutParams(width, height);
			gridView.setAdapter(new GridAdapter(width));
			gridView.setLayoutParams(lp);
			return convertView;
		}
	}
	
	private class GridAdapter extends BaseAdapter{

		private int width = 0;
		
		public GridAdapter(int width){
			this.width = width;
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return 4;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if(convertView == null){
				convertView = new ImageView(mContext);
				((ImageView)convertView).setImageResource(R.drawable.ic_designer_avatar);
				int w = (width - DisplayUtils.dipToPx(mContext, 8) * 2) / 3;
				android.widget.AbsListView.LayoutParams lp = new android.widget.AbsListView.LayoutParams(w, w);
				((ImageView)convertView).setScaleType(ScaleType.FIT_XY);
				convertView.setLayoutParams(lp);
			}
			return convertView;
		}
	}
}
